﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Collections.Generic;
using System.Web;

namespace Evpo.ContactData.CustomControls
{
    public class LookupLinkButton : LinkButton, IScriptControl
    {
        public LookupLinkButton()
        {
            CausesValidation = false;
        }

        #region Fields
        private ScriptManager _scrMgr;
        #endregion

        #region Properties
        public string HiddenElementID
        {
            get;
            set;
        }

        public string LookupName
        {
            get;
            set;
        }
        #endregion

        #region Handlers

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Page.RegisterRequiresControlState(this);
        }


       

        protected override object SaveControlState()
        {
            Pair pair = new Pair();
            pair.First = base.SaveControlState();

            pair.Second = new ControlState()
            {
                HiddenElementID = this.HiddenElementID,
                LookupName = this.LookupName
            };

            return pair;
        }

        protected override void LoadControlState(object savedState)
        {
            Pair pair = (Pair)savedState;
            base.LoadControlState(pair.First);

            ControlState state = (ControlState)pair.Second;
            HiddenElementID = state.HiddenElementID;
            LookupName = state.LookupName;
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (!this.DesignMode)
            {
                _scrMgr = ScriptManager.GetCurrent(this.Page);

                if (_scrMgr == null)
                    throw new HttpException("A ScriptManager control must exist on the page");

                _scrMgr.RegisterScriptControl(this);
            }

            base.OnPreRender(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (!this.DesignMode)
                _scrMgr.RegisterScriptDescriptors(this);

            base.Render(writer);
        }
        #endregion Handlers

        #region Methods
        public virtual IEnumerable<ScriptDescriptor> GetScriptDescriptors()
        {
            ScriptControlDescriptor descriptor = new ScriptControlDescriptor(
                "ContactData.UI.LinkButton", this.ClientID);
            descriptor.AddProperty("hiddenElementID", HiddenElementID);
            descriptor.AddProperty("lookupName", LookupName);
            return new ScriptDescriptor[] { descriptor };
        }

        public virtual IEnumerable<ScriptReference> GetScriptReferences()
        {
            ScriptReference reference = new ScriptReference();
            reference.Name = "Evpo.ContactData.CustomControls.Lookup.js";
            reference.Assembly = "Evpo.ContactData.CustomControls";
            return new ScriptReference[] { reference };
        }

        #endregion Methods

        #region Classes
        [Serializable]
        private class ControlState
        {
            internal string HiddenElementID
            {
                get;
                set;
            }

            internal string LookupName
            {
                get;
                set;
            }
        }
        #endregion Classes
    }
}
